﻿using System;
using System.Collections.Generic;
using System.Windows;
using System.Windows.Controls;
using System.Diagnostics;
using System.ComponentModel;
using System.Threading;
using System.Linq;
using System.Windows.Threading;

namespace Reserve
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private DispatcherTimer timer;

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            productionList.DisplayMemberPath = "Value";
            productionList.SelectedValuePath = "Key";

            countryList.ItemsSource = ConfigHelper.Countries;
            countryList.SelectedIndex = 0;
        }

        private void countryList_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            productionList.ItemsSource = ConfigHelper.GetProductions(countryList.SelectedValue as string);
            productionList.SelectedIndex = 0;
        }

        private void startButton_Click(object sender, RoutedEventArgs e)
        {
            startButton.IsEnabled = false;
            countryList.IsEnabled = false;
            productionList.IsEnabled = false;
            modePanel.IsEnabled = false;

            Process rundll = Process.Start("rundll32.exe", "InetCpl.cpl,ClearMyTracksByProcess 2");
            rundll.WaitForExit();

            ConfigHelper.CurrentCountry = countryList.SelectedValue as string;
            ConfigHelper.CurrentProductionID = productionList.SelectedValue as string;

            if (forceRadioButton.IsChecked == true)
            {
                AutoClickManager.StartAutoClick(this, ConfirmMode.ForceMode);
            }
            else
            {
                timer = new DispatcherTimer();
                timer.Interval = new TimeSpan(0, 0, 5);
                timer.Tick += timer_Tick;
                timer.Start();
                AutoClickManager.StartAutoClick(this, ConfirmMode.SignalMode);
            }
        }

        private void timer_Tick(object sender, EventArgs e)
        {
            logTextBlock.Text = AutoClick.WaitingCheckDoorCount.ToString();
        }

        public void GotIt()
        {
            AutoClickManager.AbortAllThread();
            if (this.timer != null)
            {
                this.timer.Stop();
            }
            this.Activate();
            MessageBox.Show("Got It!!!");
        }

        private void Window_Closing(object sender, CancelEventArgs e)
        {
            Environment.Exit(0);
        }
    }
}
